package cd.safe.school.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.os.Handler;
import cd.safe.school.bean.FileItem;

public class DownloadUpload {

	private static String ret = "";
	private static Map<String, Bitmap> caches = new HashMap<String, Bitmap>();
	private static Bitmap bit;

	// public static String upload(final String uploadurl) {
	//
	// File file = new File(Environment.getExternalStorageDirectory(),
	// "sc.png");
	// final Map<String, Object> params = new HashMap<String, Object>();
	// params.put("filename", "sc.png");
	// params.put("msg", "2");
	// params.put("infoType", "1");
	// params.put("receiveId", "2");
	// params.put("filetype", "1");
	//
	// final List<FileItem> items = new ArrayList<FileItem>();
	// FileItem fi = new FileItem();
	// fi.setFieldName("file");
	// fi.setPath(file.getAbsolutePath());
	// items.add(fi);
	//
	//
	// ret = NetTool.postFileFormParams(uploadurl, params, items);
	//
	//
	// return ret;
	// }
	//
	// public static Bitmap downloadImg(final String url) {
	//
	// if (url.indexOf("http:") == 0) {
	// bit = caches.get(url);
	// if (bit != null) {
	// return bit;
	//
	// }
	//
	// new Thread(new Runnable() {
	//
	// @Override
	// public void run() {
	// try {
	// URL u = new URL(url);
	// URLConnection uc = u.openConnection();
	// InputStream in = uc.getInputStream();
	// bit = BitmapFactory.decodeStream(in);
	// caches.put(url, bit);
	//
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	//
	// }
	// }).start();
	//
	// }
	// return bit;
	// }

	public static void downloadefile(String url, String path) {

		try {
			URL u = new URL(url);
			URLConnection conn = u.openConnection();
			InputStream in = conn.getInputStream();
			File file = new File(path);
			if (!file.exists())
				file.mkdirs();
			FileOutputStream out = new FileOutputStream(file);
			byte[] buffer = new byte[1024];
			int len = -1;
			while ((len = in.read(buffer)) > 0) {
				out.write(buffer, 0, len);
				out.flush();
			}

			out.close();
			in.close();

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
